package com.gjm.demo.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * 实现自定义注解
 */
@Slf4j
@Aspect
@Component
public class MyAspectDemo {

    /**
     * 实现注解 MyAspect
     * @param proceedingJoinPoint
     * @return
     */
//    @Around("@annotation(org.springframework.web.bind.annotation.RequestMapping)") //加了@RequestMapping就生效
    @Around("@annotation(com.gjm.demo.aspect.MyAspect)") //只有加了MyAspect的注解才生效
    public Object recordTime(ProceedingJoinPoint proceedingJoinPoint) {
        log.info("around before");

        Object result = null;

        long startTime = System.currentTimeMillis();

        try {
            result = proceedingJoinPoint.proceed();
        } catch (Throwable e) {
//            throw new RuntimeException(e);
            log.error("throwing");
        }

        long endTime = System.currentTimeMillis();

        log.info("方法耗时: {}", (endTime - startTime) + "ms");

        log.info("around after");

        return result;
    }
}
